package cn.com.paycn.ken.dao.impl;

import cn.com.paycn.ken.dao.BaseDao;
import cn.com.paycn.ken.utils.ResourceUtils;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

@Repository("baseDao")
public class BaseDaoImpl implements BaseDao {
	 @Resource(name="sessionFactory")
	  private SessionFactory sessionFactory;

	  private int page(Integer page, Integer rows)
	  {
	    if (page == null) {
	      page = Integer.valueOf(1);
	    }
	    return (page.intValue() - 1) * rows.intValue();
	  }

	  private Query createQuery(String hql) {
	    return getSession().createQuery(hql);
	  }

	  private Query createQueryWithPage(String hql, Integer page, Integer rows) {
	    if (rows == null) {
	      rows = ResourceUtils.DEFAULT_ROWS;
	    }
	    return getSession().createQuery(hql)
	      .setFirstResult(page(page, rows))
	      .setMaxResults(rows.intValue());
	  }

	  public Session getSession() {
	    return this.sessionFactory.getCurrentSession();
	  }

	  public Serializable save(Object o) {
	    Session s = getSession();
	    Serializable ser = s.save(o);
	    s.flush();
	    return ser;
	  }

	  public void update(Object o) {
	    getSession().update(o);
	  }

	  public int update(String hql) {
	    return createQuery(hql).executeUpdate();
	  }

	  public int update(String hql, Object[] args) {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.executeUpdate();
	  }

	  public int update(String hql, List<String> args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.size(); i++) {
	      query.setParameter(i, args.get(i));
	    }
	    return query.executeUpdate();
	  }

	  public int update(String hql, Map<String, Object> args)
	  {
	    return createQuery(hql).setProperties(args).executeUpdate();
	  }

	  public void saveOrUpdate(Object o)
	  {
	    getSession().saveOrUpdate(o);
	  }

	  public Object get(String hql, Map<String, Object> args)
	  {
	    return createQuery(hql).setProperties(args).uniqueResult();
	  }

	  public Object get(String hql, Object[] args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.uniqueResult();
	  }

	  public <T> T get(Class<T> clazz, Serializable id)
	  {
	    return (T) getSession().get(clazz, id);
	  }

	  public List find(String hql)
	  {
	    return createQuery(hql).list();
	  }

	  public List find(String hql, Object[] args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.list();
	  }

	  public List find(String hql, Map<String, Object> args)
	  {
	    return createQuery(hql).setProperties(args).list();
	  }

	  public List find(String hql, Integer page, Integer rowsCount)
	  {
	    return createQueryWithPage(hql, page, rowsCount).list();
	  }

	  public List find(String hql, Integer page, Integer rowsCount, List<Object> args)
	  {
	    Query query = createQueryWithPage(hql, page, rowsCount);
	    for (int i = 0; i < args.size(); i++) {
	      query.setParameter(i, args.get(i));
	    }
	    return query.list();
	  }

	  public List find(String hql, Integer page, Integer rowsCount, Map<String, Object> args)
	  {
	    return createQueryWithPage(hql, page, rowsCount)
	      .setProperties(args).list();
	  }

	  public Long count(String hql)
	  {
	    return (Long)createQuery(hql).uniqueResult();
	  }

	  public Long count(String hql, Object[] args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return (Long)query.uniqueResult();
	  }

	  public Long count(String hql, List<Object> args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.size(); i++) {
	      query.setParameter(i, args.get(i));
	    }
	    Object resu = query.uniqueResult();
	    return (Long)query.uniqueResult();
	  }

	  public Long count(String hql, Map<String, Object> args)
	  {
	    Query query = createQuery(hql).setProperties(args);
	    return (Long)query.uniqueResult();
	  }

	  public void delete(Object o)
	  {
	    getSession().delete(o);
	  }

	  public int delete(String hql, Object[] args)
	  {
	    Query query = createQuery(hql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.executeUpdate();
	  }

	  public int delete(String hql, Map<String, Object> args)
	  {
	    return createQuery(hql).setProperties(args).executeUpdate();
	  }

	  public int deleteBySQL(String sql, Object[] args)
	  {
	    SQLQuery query = getSession().createSQLQuery(sql);
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.executeUpdate();
	  }

	  public Object first(String hql)
	  {
	    return createQueryWithPage(hql, Integer.valueOf(1), Integer.valueOf(1)).uniqueResult();
	  }

	  public Object first(String hql, Object[] args)
	  {
	    Query query = createQueryWithPage(hql, Integer.valueOf(1), Integer.valueOf(1));
	    for (int i = 0; i < args.length; i++) {
	      query.setParameter(i, args[i]);
	    }
	    return query.uniqueResult();
	  }

	  public Object first(String hql, Map<String, Object> args)
	  {
	    return createQueryWithPage(hql, Integer.valueOf(1), Integer.valueOf(1)).setProperties(args).uniqueResult();
	  }

}
